﻿namespace Chain.IDAL
{
    using Chain.DBUtility;
    using Chain.Model;
    using System;
    using System.Data;
    using System.Data.SqlClient;
    using System.Text;

    public class GiftClass
    {
        public int Add(Chain.Model.GiftClass model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("insert into GiftClass(");
            builder.Append("GiftClassName,GiftClassRemark,GiftParentID)");
            builder.Append(" values (");
            builder.Append("@GiftClassName,@GiftClassRemark,@GiftParentID)");
            builder.Append(";select @@IDENTITY");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@GiftClassName", SqlDbType.VarChar, 50), new SqlParameter("@GiftClassRemark", SqlDbType.NVarChar, 100), new SqlParameter("@GiftParentID", SqlDbType.Int, 4) };
            cmdParms[0].Value = model.GiftClassName;
            cmdParms[1].Value = model.GiftClassRemark;
            cmdParms[2].Value = model.GiftParentID;
            object single = DbHelperSQL.GetSingle(builder.ToString(), cmdParms);
            if (single == null)
            {
                return 0;
            }
            return Convert.ToInt32(single);
        }

        public bool Delete(int GiftClassID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from GiftClass ");
            builder.Append(" where GiftClassID=@GiftClassID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@GiftClassID", SqlDbType.Int, 4) };
            cmdParms[0].Value = GiftClassID;
            return (DbHelperSQL.ExecuteSql(builder.ToString(), cmdParms) > 0);
        }

        public int DeleteByParentID(int ParentID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from GiftClass ");
            builder.Append(" where GiftParentID=@GiftParentID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@GiftParentID", SqlDbType.Int, 4) };
            cmdParms[0].Value = ParentID;
            if (DbHelperSQL.ExecuteSql(builder.ToString(), cmdParms) > 0)
            {
                return 1;
            }
            return -3;
        }

        public bool DeleteList(string GiftClassIDlist)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from GiftClass ");
            builder.Append(" where GiftClassID in (" + GiftClassIDlist + ")  ");
            return (DbHelperSQL.ExecuteSql(builder.ToString()) > 0);
        }

        public bool Exists(int GiftClassID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select count(1) from GiftClass");
            builder.Append(" where GiftClassID=@GiftClassID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@GiftClassID", SqlDbType.Int, 4) };
            cmdParms[0].Value = GiftClassID;
            return DbHelperSQL.Exists(builder.ToString(), cmdParms);
        }

        public bool Exists(string ClassName, int classID)
        {
            StringBuilder builder = new StringBuilder();
            builder.AppendFormat(" select GiftClassID,GiftClassName from GiftClass where GiftClassID not in ({0}) and GiftClassName ='{1}'", classID, ClassName);
            return DbHelperSQL.Exists(builder.ToString());
        }

        public DataSet GetItem(int ClassID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select * from GiftClass ");
            builder.Append(" where GiftClassID=@GiftClassID ");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@GiftClassID", SqlDbType.Int, 4) };
            cmdParms[0].Value = ClassID;
            return DbHelperSQL.Query(builder.ToString(), cmdParms);
        }

        public DataSet GetList(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select GiftClassID,GiftClassName,GiftClassRemark,GiftParentID ");
            builder.Append(" FROM GiftClass ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(builder.ToString());
        }

        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select ");
            if (Top > 0)
            {
                builder.Append(" top " + Top.ToString());
            }
            builder.Append(" GiftClassID,GiftClassName,GiftClassRemark,GiftParentID ");
            builder.Append(" FROM GiftClass ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            builder.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(builder.ToString());
        }

        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("SELECT * FROM ( ");
            builder.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                builder.Append("order by T." + orderby);
            }
            else
            {
                builder.Append("order by T.GiftClassID desc");
            }
            builder.Append(")AS Row, T.*  from GiftClass T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                builder.Append(" WHERE " + strWhere);
            }
            builder.Append(" ) TT");
            builder.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(builder.ToString());
        }

        public int GetMaxId()
        {
            return DbHelperSQL.GetMaxID("GiftClassID", "GiftClass");
        }

        public Chain.Model.GiftClass GetModel(int GiftClassID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select  top 1 GiftClassID,GiftClassName,GiftClassRemark,GiftParentID from GiftClass ");
            builder.Append(" where GiftClassID=@GiftClassID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@GiftClassID", SqlDbType.Int, 4) };
            cmdParms[0].Value = GiftClassID;
            Chain.Model.GiftClass class2 = new Chain.Model.GiftClass();
            DataSet set = DbHelperSQL.Query(builder.ToString(), cmdParms);
            if (set.Tables[0].Rows.Count <= 0)
            {
                return null;
            }
            if ((set.Tables[0].Rows[0]["GiftClassID"] != null) && (set.Tables[0].Rows[0]["GiftClassID"].ToString() != ""))
            {
                class2.GiftClassID = int.Parse(set.Tables[0].Rows[0]["GiftClassID"].ToString());
            }
            if ((set.Tables[0].Rows[0]["GiftClassName"] != null) && (set.Tables[0].Rows[0]["GiftClassName"].ToString() != ""))
            {
                class2.GiftClassName = set.Tables[0].Rows[0]["GiftClassName"].ToString();
            }
            if ((set.Tables[0].Rows[0]["GiftClassRemark"] != null) && (set.Tables[0].Rows[0]["GiftClassRemark"].ToString() != ""))
            {
                class2.GiftClassRemark = set.Tables[0].Rows[0]["GiftClassRemark"].ToString();
            }
            if ((set.Tables[0].Rows[0]["GiftParentID"] != null) && (set.Tables[0].Rows[0]["GiftParentID"].ToString() != ""))
            {
                class2.GiftParentID = int.Parse(set.Tables[0].Rows[0]["GiftParentID"].ToString());
            }
            return class2;
        }

        public int GetRecordCount(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select count(1) FROM GiftClass ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            object single = DbHelperSQL.GetSingle(builder.ToString());
            if (single == null)
            {
                return 0;
            }
            return Convert.ToInt32(single);
        }

        public int Update(Chain.Model.GiftClass model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("update GiftClass set ");
            builder.Append("GiftClassName=@GiftClassName,");
            builder.Append("GiftClassRemark=@GiftClassRemark,");
            builder.Append("GiftParentID=@GiftParentID");
            builder.Append(" where GiftClassID=@GiftClassID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@GiftClassName", SqlDbType.VarChar, 50), new SqlParameter("@GiftClassRemark", SqlDbType.NVarChar, 100), new SqlParameter("@GiftParentID", SqlDbType.Int, 4), new SqlParameter("@GiftClassID", SqlDbType.Int, 4) };
            cmdParms[0].Value = model.GiftClassName;
            cmdParms[1].Value = model.GiftClassRemark;
            cmdParms[2].Value = model.GiftParentID;
            cmdParms[3].Value = model.GiftClassID;
            int num = DbHelperSQL.ExecuteSql(builder.ToString(), cmdParms);
            if (num > 0)
            {
                return num;
            }
            return 0;
        }
    }
}

